﻿@page "/test/page/{id}"
@page "/test/page1/{id}"
@inherits BasePage
@attribute [DisplayName("路由页面")]

<h1>@Id</h1>
<AntSelectCode DataSource="Texts" Placeholder="请选择测试文本" />
<Button OnClick="OnClick">弹窗</Button>
<Button OnClick="ShowEnumForm">显示Enum字段表单</Button>
<Button OnClick="ShowDictionaryForm">显示Dictionary表单</Button>
<TestForm @ref="form" Model="model" />
<Button Type="@ButtonType.Primary" OnClick="OnSave">确定</Button>

<Button Type="primary" @onclick="_=>open()">Open</Button>
<Drawer Closable="true" @bind-Visible="visible" Placement="bottom" Title='("Basic Drawer")'>
    <p>Some contents...</p>
    <p>Some contents...</p>
    <p>Some contents...</p>
</Drawer>

@code {
    private List<CodeInfo> Texts = TestHelper.GetTexts();
    private FormModel<TestInfo> model;
    private TestForm form;
    bool visible = false;

    void open()
    {
        this.visible = true;
    }

    protected override async Task OnInitPageAsync()
    {
        await base.OnInitPageAsync();
        model = new FormModel<TestInfo>(this);
        model.Title = "测试窗体";
        model.Info = new FormInfo { Width = 800 };
        model.Type = typeof(TestForm);
        model.Data = new TestInfo();
        model.Footer = null;
        model.OnSave = d => Known.Result.SuccessAsync(d.Text, d);
    }

    private void OnClick()
    {
        UI.ShowForm(model);
    }

    private void OnSave() => model.SaveAsync();

    private void ShowEnumForm()
    {
        var form = new FormModel<TestEntity>(this)
        {
            Title = "枚举实体",
            Data = new TestEntity(),
            OnSave = d =>
            {
                UI.Alert(d.Type.ToString());
                return Known.Result.SuccessAsync("保存成功！");
            }
        };
        form.AddRow().AddColumn(c => c.Name);
        form.AddRow().AddColumn(c => c.Type, c => c.Type = FieldType.RadioList);
        UI.ShowForm(form);
    }

    private void ShowDictionaryForm()
    {
        var data = new Dictionary<string, object>
        {
            ["Id"] = 1,
            ["Name"] = ""
        };
        var form = new FormModel<Dictionary<string, object>>(this)
        {
            Title = "字典表单",
            Data = data,
            OnSave = d =>
            {
                UI.Alert(d["Name"].ToString());
                return Known.Result.SuccessAsync("保存成功！");
            }
        };
        form.AddRow().AddColumn(new ColumnInfo { Id = "Id", Label = "编号", Type = FieldType.Number });
        form.AddRow().AddColumn(new ColumnInfo { Id = "Name", Label = "名称", Type = FieldType.Text });
        UI.ShowForm(form);
    }
}